//
//  MeView.m
//  mendianjieke
//
//  Created by 响当当 on 8/2/19.
//  Copyright © 2019 dongzheng. All rights reserved.
//

#import "MeView.h"
#import "HeadInfoCell.h"
#import "PersonInformationCell.h"
#import "AdvertisementCell.h"
#import "ActivityCell.h"
#import "SettingCell.h"
#import "FaceAttestationViewController.h"
#import "SettingViewController.h"

@interface MeView()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)UIView *footView;
@property (nonatomic, strong)NSMutableArray *settingArr;
@property (nonatomic, strong)NSArray *activityArr;
@property (nonatomic, strong)NSIndexPath *indexPath;
@end

@implementation MeView

-(NSArray *)activityArr
{
    if (!_activityArr)
    {
        _activityArr = [[NSArray alloc]init];
    }
    return _activityArr;
}

- (NSMutableArray *)settingArr
{
    if (!_settingArr)
    {
        _settingArr = [[NSMutableArray alloc]init];
//        NSArray *arr = @[@"员工人脸认证",@"卡券核销",@"TIMING平台",@"客户个性化中心",@"设置"];
//        NSArray *imageArr = @[@"staff_face",@"ticket_writeoff",@"timing_platform",@"market_center",@"personal_setting"];
        
        NSArray *arr = @[@"员工人脸认证",@"设置"];
        NSArray *imageArr = @[@"staff_face",@"personal_setting"];
        for (int i=0; i<arr.count; i++)
        {
            SettingModel *model = [[SettingModel alloc]init];
            model.title = arr[i];
            model.image = imageArr[i];
            model.row = [NSString stringWithFormat:@"%d",i];
            [_settingArr addObject:model];
        }
    }
    return _settingArr;
}

- (UIView *)footView
{
    if (!_footView)
    {
        _footView = [[UIView alloc]initWithFrame:CGRectMake(0,125*kUIScaleBasePlus,15*kUIScaleBasePlus,20*kUIScaleBasePlus)];
        _footView.backgroundColor = LCHexColor(0xff9f00);
        
    }
    return _footView;
}

- (UITableView *)tableView
{
    if (!_tableView)
    {
        _tableView = [[UITableView alloc]initWithFrame:self.bounds style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = 0;
        _tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
        _tableView.showsVerticalScrollIndicator = NO;
        
        MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
              
              // 设置文字
              [header setTitle:@"加载更多" forState:MJRefreshStateIdle];
              [header setTitle:@"下拉刷新" forState:MJRefreshStatePulling];
//              [header setTitle:@"加载中" forState:MJRefreshStateRefreshing];
              
              // 设置字体
              header.stateLabel.font = [UIFont systemFontOfSize:15];
              header.lastUpdatedTimeLabel.font = [UIFont systemFontOfSize:12];
              
              // 设置颜色
              header.stateLabel.textColor = LCHexColor(0x989898);
              header.lastUpdatedTimeLabel.textColor = LCHexColor(0x989898);
              
              // 设置刷新控件
              self.tableView.mj_header = header;

    }
    return _tableView;
}


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self addSubview:self.tableView];
        
        UIView *leftCView = [[UIView alloc]initWithFrame:CGRectMake(0,125*kUIScaleBasePlus,15*kUIScaleBasePlus,20*kUIScaleBasePlus)];
        leftCView.backgroundColor = LCHexColor(0xfd9047);
        [self.tableView addSubview:leftCView];
        
//        UIBezierPath *leftmaskCPath = [UIBezierPath bezierPathWithRoundedRect:leftCView.bounds byRoundingCorners:UIRectCornerTopRight cornerRadii:CGSizeMake(10,10)];
//        CAShapeLayer *leftmaskCLayer = [[CAShapeLayer alloc] init];
//        leftmaskCLayer.frame = leftCView.bounds;
//        leftmaskCLayer.path = leftmaskCPath.CGPath;
//        leftCView.layer.mask = leftmaskCLayer;
        
        UIView *leftView = [[UIView alloc]initWithFrame:CGRectMake(10*kUIScaleBasePlus, 130*kUIScaleBasePlus,100*kUIScaleBasePlus,15*kUIScaleBasePlus)];
        leftView.backgroundColor = [UIColor whiteColor];
        [self.tableView addSubview:leftView];
        
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:leftView.bounds byRoundingCorners:UIRectCornerTopLeft cornerRadii:CGSizeMake(10,10)];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = leftView.bounds;
        maskLayer.path = maskPath.CGPath;
        leftView.layer.mask = maskLayer;
        
        UIView *rightView = [[UIView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-15*kUIScaleBasePlus,125*kUIScaleBasePlus,15*kUIScaleBasePlus,20*kUIScaleBasePlus)];
        rightView.backgroundColor = LCHexColor(0xfd9047);
        [self.tableView addSubview:rightView];
        
        
        UIView *rightCView = [[UIView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH -110*kUIScaleBasePlus, 130*kUIScaleBasePlus,100*kUIScaleBasePlus,15*kUIScaleBasePlus)];
        rightCView.backgroundColor = [UIColor whiteColor];
        [self.tableView addSubview:rightCView];
        
        UIBezierPath *maskCPath = [UIBezierPath bezierPathWithRoundedRect:rightCView.bounds byRoundingCorners:UIRectCornerTopRight cornerRadii:CGSizeMake(10,10)];
        CAShapeLayer *maskCLayer = [[CAShapeLayer alloc] init];
        maskCLayer.frame = rightCView.bounds;
        maskCLayer.path = maskCPath.CGPath;
        rightCView.layer.mask = maskCLayer;
    }
    return self;
}

#pragma mark - tableView delegate & datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 5;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    if (section == 4)
    {
        return self.settingArr.count;
    }
    else
    {
        return 1;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 4)
    {
        return 50*kUIScaleBasePlus;
    }
    else if (indexPath.section == 0)
    {
        return 130*kUIScaleBasePlus;
    }
    else if (indexPath.section == 2)
    {
        return 94*kUIScaleBasePlus;
    }
    else
    {
        return 150*kUIScaleBasePlus;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0 )
    {
       return 0.01*kUIScaleBasePlus;
    }
    else if(section == 1 )
    {
        return 0.01*kUIScaleBasePlus;
    }
    else
    {
        return 5*kUIScaleBasePlus;
    }
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 1)
    {
        UIView *footView = [[UIView alloc]initWithFrame:CGRectMake(0,0,SCREEN_WIDTH,20*kUIScaleBasePlus)];
        footView.backgroundColor = LCHexColor(0xff9f00);
        return footView;
    }
    return [UIView new];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *backcell = nil;
    
    switch (indexPath.section)
    {
        case 0:
        {
            HeadInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HeadInfoCell"];
            if (!cell)
            {
                cell = [[HeadInfoCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"HeadInfoCell"];
                cell.selectionStyle = 0;
            }
            if (self.model)
            {
                cell.model = self.model;
            }
            
            WeakSelf(self)
            cell.changeStateBlock = ^() {
              [weakSelf changeSwitchState];
            };
            
            self.indexPath = indexPath;
            backcell = cell;
        }
            break;
        case 1:
        {
            PersonInformationCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PersonInformationCell"];
            if (!cell)
            {
                cell = [[PersonInformationCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"PersonInformationCell"];
                cell.selectionStyle = 0;
            }
            if (self.model)
            {
                cell.model = self.model;
            }
            
            backcell = cell;
        }
            break;
        case 2:
        {
            AdvertisementCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AdvertisementCell"];
            if (!cell)
            {
                cell = [[AdvertisementCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"AdvertisementCell"];
                cell.selectionStyle = 0;
            }
            
            backcell = cell;
        }
            break;
        case 3:
        {
            ActivityCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ActivityCell"];
            if (!cell)
            {
                cell = [[ActivityCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ActivityCell"];
                cell.selectionStyle = 0;
            }
            
            if (self.activityArr.count!=0)
            {
                [cell getActivityInfo:self.activityArr];
            }
            else
            {
                [cell getNoActivityInfo];
            }
            backcell = cell;
        }
            break;
        case 4:
        {
            SettingCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SettingCell"];
            if (!cell)
            {
                cell = [[SettingCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SettingCell"];
                cell.selectionStyle = 0;
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            }
            
            cell.model = self.settingArr[indexPath.row];
            
            backcell = cell;
        }
            break;
    }
    
    return backcell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 4)
    {
        switch (indexPath.row)
        {
            case 0:
            {
                FaceAttestationViewController *vc = [[FaceAttestationViewController alloc]init];
                vc.hidesBottomBarWhenPushed = YES;
                vc.isFaceTure = @"0";
                [[Utilties getCurrentVC].navigationController pushViewController:vc animated:YES];
            }
                break;
            case 1:
            {
                SettingViewController *vc = [[SettingViewController alloc]init];
                vc.hidesBottomBarWhenPushed = YES;
                [[Utilties getCurrentVC].navigationController pushViewController:vc animated:YES];
//                DISMISS_SVP(1.0)
//                [SVProgressHUD showInfoWithStatus:@"敬请期待"];
            }
                break;
            case 2:
            {
                DISMISS_SVP(1.0)
                [SVProgressHUD showInfoWithStatus:@"敬请期待"];
            }
                break;
            case 3:
            {
                DISMISS_SVP(1.0)
                [SVProgressHUD showInfoWithStatus:@"敬请期待"];
            }
                break;
            case 4:
            {
                SettingViewController *vc = [[SettingViewController alloc]init];
                vc.hidesBottomBarWhenPushed = YES;
                [[Utilties getCurrentVC].navigationController pushViewController:vc animated:YES];
            }
                break;
            default:
                break;
        }
    }
}

- (void)setModel:(UserInfoModel *)model
{
    _model = model;
    [self.tableView reloadData];
   dispatch_async(dispatch_get_main_queue(), ^{
          NSIndexPath * dayOne = [NSIndexPath indexPathForRow:0 inSection:0];
          [self.tableView scrollToRowAtIndexPath:dayOne atScrollPosition:UITableViewScrollPositionTop animated:NO];
      });
}

- (void)getActivityInfo:(NSArray *)arr
{
    self.activityArr = arr;
    [self.tableView reloadData];
    dispatch_async(dispatch_get_main_queue(), ^{
           NSIndexPath * dayOne = [NSIndexPath indexPathForRow:0 inSection:0];
           [self.tableView scrollToRowAtIndexPath:dayOne atScrollPosition:UITableViewScrollPositionTop animated:NO];
       });
}

- (void)changeSwitchState
{
    HeadInfoCell *cell = [self.tableView cellForRowAtIndexPath:self.indexPath];
    NSString *str = LCUserDefaultsGet(@"switch");
    [cell changeState:str];
}


#pragma mark - Method
- (void)loadNewData
{
    WeakSelf(self)
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (self.reloadDataBlock)
        {
            self.reloadDataBlock();
        }
        [weakSelf.tableView.mj_header endRefreshing];
    });
}

@end

